package com.jh3502.vivo.function.tododetail.reim;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jh3502.vivo.R;
import com.jh3502.vivo.function.reimnew.beans.CategoryInfoBean;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author:harisucici on 2018/11/14 11:33
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class TodoReimAdapter extends RecyclerView.Adapter<TodoReimAdapter.ViewHolder> implements View.OnClickListener {

    Context mContext;

    private View convertView;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<TodoReimBean.ExpenseBean.EdInfoBean> dataList;
    private TodoReimItemAdapter mAdapter;

    public TodoReimAdapter(Context context, List<TodoReimBean.ExpenseBean.EdInfoBean> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_reim_new, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        CategoryInfoBean cb = new Gson().fromJson(dataList.get(position).getCategoryinfo(), CategoryInfoBean.class);
        viewHolder.newDate.setText(dataList.get(position).getOccursDate());
        viewHolder.newPlace.setText(dataList.get(position).getCityName());
        mAdapter = new TodoReimItemAdapter(mContext, cb);
        viewHolder.newRecycler.setAdapter(mAdapter);
        double applyMoney = dataList.get(position).getApplyMoney();
        double actualMoney = dataList.get(position).getActualMoney();
        viewHolder.newTotalApply.setText(applyMoney + "");
        viewHolder.newTotalOut.setText(Utils.getInstance().sub(applyMoney, actualMoney) + "");
        viewHolder.newTotalActual.setText(actualMoney + "");
        viewHolder.newAdd.setVisibility(View.GONE);


    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {

            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        if(dataList==null)
            return 0;
        return dataList.size();
    }


    public interface ButtonInterface {
        void onClick(int position);
    }


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int i);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.new_date)
        TextView newDate;
        @BindView(R.id.new_place)
        TextView newPlace;
        @BindView(R.id.new_add)
        Button newAdd;
        @BindView(R.id.new_total_apply)
        TextView newTotalApply;
        @BindView(R.id.new_total_out)
        TextView newTotalOut;
        @BindView(R.id.new_total_actual)
        TextView newTotalActual;
        @BindView(R.id.new_recycler)
        MyRecyclerView newRecycler;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

}
